package cn.itcast.tanhua.mytanhuaserver.service.impl;

import cn.itcast.tanhua.mytanhuadubbointerface.api.QuanziApi;
import cn.itcast.tanhua.mytanhuaserver.service.ManualCheckService;
import cn.itcast.tanhua.pojo.Publish;
import cn.itcast.tanhua.pojo.User;
import cn.itcast.tanhua.utils.UserThreadLocal;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.aliyunpicuploadspringbootstart.service.PicUploadService;
import com.itheima.aliyunpicuploadspringbootstart.vo.PicUploadResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 陈功炳
 * @since 2021/12/6 19:06
 */
@Service
public class ManualCheckServiceImpl implements ManualCheckService {

    @Autowired
    private PicUploadService picUploadService;

    @Reference
    private QuanziApi quanziApi;

    /**
     * 添加有问题的动态到publish表 新增字段state状态为0 -> 审核中
     * @param textContent
     * @param location
     * @param latitude
     * @param longitude
     * @param multipartFile
     * @return
     */
    @Override
    public String checkLetter(String textContent, String location, String latitude, String longitude, MultipartFile[] multipartFile) {
        //获取用户id
        User user = UserThreadLocal.get();

        //封装publish
        Publish publish = new Publish();
        publish.setUserId(user.getId());
        publish.setText(textContent);
        publish.setLocationName(location);
        publish.setLatitude(latitude);
        publish.setLongitude(longitude);
        publish.setSeeType(1);
        //0 => 审核中
        publish.setState(0);

        List<String> picUrls = new ArrayList<>();
        //图片上传
        for (MultipartFile file : multipartFile) {
            PicUploadResult picUploadResult = this.picUploadService.upload(file);
            picUrls.add(picUploadResult.getName());
        }
        publish.setMedias(picUrls);

        String publishId = quanziApi.savePublish(publish);

        return publishId;
    }
}


